﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using GPSTracker.Helper;
using System.Data;
using System.Data.SqlClient;

namespace GPSTracker
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Cookies["delivery_username"] != null && Request.Cookies["delivery_password"] != null)
                {
                    tbUserName.Text = Request.Cookies["delivery_username"].Value;
                    tbPassword.Text = Request.Cookies["delivery_password"].Value.ToString();
                    tbPassword.Attributes.Add("value", Request.Cookies["delivery_password"].Value.ToString());
                    tbPassword.DataBind();
                }
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            var strUserName = tbUserName.Text;
            var strUserPass = tbPassword.Text;

            if (String.IsNullOrEmpty(strUserName) || String.IsNullOrEmpty(strUserPass))
            {
                return;
            }

            if (this.CheckLogin(strUserName, strUserPass))
            {
                if (chk_remember.Checked == true)
                {
                    Response.Cookies["delivery_username"].Value = tbUserName.Text;
                    Response.Cookies["delivery_username"].Expires = DateTime.Now.AddDays(3);
                    Response.Cookies["delivery_password"].Value = tbPassword.Text;
                    Response.Cookies["delivery_password"].Expires = DateTime.Now.AddDays(3);
                }
                Response.Redirect("../Default.aspx");
            }
            else
            {
                var lg = Session["LoginMaster"] as Views.DAL.LoginBO;
                ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('UserName Or Password Incorrect.');", true);
            }
        }

        private bool CheckLogin(string loginName, string pwd)
        {
            try
            {
                var cmd = new SqlCommand();
                cmd.Connection = CommonFx.GetSqlCon();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "SP_CheckLogin";

                cmd.Parameters.Add(new SqlParameter("@pLoginName", loginName));
                cmd.Parameters.Add(new SqlParameter("@pPwd", pwd));
                cmd.Parameters.Add(new SqlParameter("@pCallType", "WEB"));
                cmd.Parameters.Add(new SqlParameter("@pCallFrom", "Login Page"));

                cmd.Parameters.Add("@pIsErrorFound", SqlDbType.Bit);
                cmd.Parameters.Add("@pErrorMessage", SqlDbType.NVarChar, 4000);

                cmd.Parameters["@pIsErrorFound"].Direction = ParameterDirection.Output;
                cmd.Parameters["@pErrorMessage"].Direction = ParameterDirection.Output;

                var adp = new SqlDataAdapter(cmd);
                var dt = new DataTable();
                adp.Fill(dt);

                if (Convert.ToBoolean(cmd.Parameters["@pIsErrorFound"].Value) == false)
                {
                    if (dt != null)
                    {
                        if (dt.Rows.Count == 1)
                        {
                            var loginBO = new Views.DAL.LoginBO();
                            loginBO.LoginID = Convert.ToInt32(dt.Rows[0]["LoginID"]);
                            loginBO.LoginName = dt.Rows[0]["LoginName"].ToString();
                            loginBO.LoginPwd = dt.Rows[0]["Password"].ToString();

                            loginBO.FirstName = dt.Rows[0]["FirstName"].ToString();
                            loginBO.MiddleName = dt.Rows[0]["MiddleName"].ToString();
                            loginBO.LastName = dt.Rows[0]["LastName"].ToString();
                            loginBO.DisplayName = dt.Rows[0]["DisplayName"].ToString();

                            loginBO.DesignationID = Convert.ToInt32(dt.Rows[0]["DesignationID"]);
                            loginBO.DesignationName = dt.Rows[0]["DesignationName"].ToString();
                            loginBO.DesignationValue = dt.Rows[0]["DesignationValue"].ToString();

                            loginBO.MobileNo1 = dt.Rows[0]["MobileNo1"].ToString();
                            loginBO.MobileNo2 = dt.Rows[0]["MobileNo2"].ToString();
                            loginBO.EmailID = dt.Rows[0]["EmailID"].ToString();

                            loginBO.AdharCardNo = dt.Rows[0]["AdharCardNo"].ToString();
                            loginBO.DrivingLicenseNo = dt.Rows[0]["DrivingLicenseNo"].ToString();
                            loginBO.PanCardNo = dt.Rows[0]["PanCardNo"].ToString();
                            loginBO.ResidenceAddress = dt.Rows[0]["ResidenceAddress"].ToString();

                            loginBO.IsAdmin = Convert.ToBoolean(dt.Rows[0]["IsAdmin"]);
                            loginBO.IsBlock = Convert.ToBoolean(dt.Rows[0]["IsBlock"]);
                            loginBO.IsMobileUser = Convert.ToBoolean(dt.Rows[0]["IsMobileUser"]);
                            loginBO.IsWebUser = Convert.ToBoolean(dt.Rows[0]["IsWebUser"]);

                            loginBO.BranchID = Convert.ToInt32(dt.Rows[0]["BranchID"]);
                            loginBO.BranchCode = dt.Rows[0]["BranchCode"].ToString();
                            loginBO.WarehouseID = Convert.ToInt32(dt.Rows[0]["WarehouseID"]);
                            loginBO.WhouseCode = dt.Rows[0]["WhouseCode"].ToString();

                            Session["LoginMaster"] = loginBO;

                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }
    }
}
